package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.SmartBeep;
import com.moleq.device.DeviceManager;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.models.GlobalDiscountSetting;
import com.moleq.models.GlobalInfo;
import com.moleq.service.ServiceManager;

public class TransDiscountDialog extends MoleQBaseDialog {

	private Button mButtonBack,mButtonOk,mButtonRemove;
	private TransactionManager mPaymentMgr;
	private ListView mListView;
	private List<GlobalDiscountSetting> mDataSource = new ArrayList<GlobalDiscountSetting>();
	private OpenDiscountAdapter mAdapter;
	private DeviceManager mDeviceManager; //contral device scanner or scale
	private int selectedRow = 0;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.buttonDiscountSelectionClose:
				mDeviceManager.enableAllScanner();
				dismiss();
				break;
			case R.id.buttonDiscountSelectionremove:
				
				MessageBox.show(mContext, "Do you want to remove the subtotal discount?",mOnClickMessageListener,MessageDialog.MESSAGE_BOX_YES_NO);
				break;
			case R.id.buttonDiscountSelectionOk:
				
				if (mDataSource.size() > 0) {
					GlobalDiscountSetting discount = mDataSource.get(selectedRow);
					mPaymentMgr.TransDntType = "P";
					mPaymentMgr.TransDntValue = discount.getGd_value();
					mPaymentMgr.mSaleListItems.announceCartChanges();
					mDeviceManager.enableAllScanner();
					GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
					TransactionManager.getInstance().SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, discount.getGd_value());
					dismiss();
				}	
				break;
			}
		}
	};
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {


		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			switch(parent.getId()){
			case R.id.lv_dialog_discount_list:
				ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public TransDiscountDialog(Context context) {
		super(context,R.style.dialog,context);
	}

	public TransDiscountDialog(Context context, int theme) {
		super(context, theme);
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.dialog_discount_selection);
		this.setCancelable(false);
		mDeviceManager = DeviceManager.getInstance();
		mPaymentMgr =  TransactionManager.getInstance();
	
		mButtonBack = (Button) findViewById(R.id.buttonDiscountSelectionClose);
		mButtonBack.setOnClickListener(mOnClickListener);
		mButtonOk = (Button) findViewById(R.id.buttonDiscountSelectionOk);
		mButtonOk.setOnClickListener(mOnClickListener);
		mButtonRemove = (Button) findViewById(R.id.buttonDiscountSelectionremove);
		mButtonRemove.setOnClickListener(mOnClickListener);
		mListView = (ListView) findViewById(R.id.lv_dialog_discount_list);
		mListView.setOnItemClickListener(onItemClickListener);
		
		mAdapter = new OpenDiscountAdapter(mContext);
		mListView.setAdapter(mAdapter);
		mListView.setDivider(null);

	}
	
	public void show(){
		super.show();
		
		mDeviceManager.disableAllScanner();
		mDataSource = ServiceManager.getInstance().mGlobalDiscountSettingService.FindAll();
		mAdapter.notifyDataSetChanged();
	}
	
	

	class OpenDiscountAdapter extends BaseAdapter{
		private Context maContext;
		public final class ViewHolder{
			public TextView tv_discount_name;
			public TextView tv_discount_value;
		}
		
		public OpenDiscountAdapter(Context context){
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				
				convertView = LayoutInflater.from(maContext).inflate(R.layout.discount_list_item, null);
				holder.tv_discount_name = (TextView) convertView.findViewById(R.id.tv_discount_name);
				holder.tv_discount_value = (TextView) convertView.findViewById(R.id.tv_discount_value);
	
				convertView.setOnTouchListener(onItemTouchListener);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			String content = mDataSource.get(position).getGd_desc();
			String value = String.valueOf(mDataSource.get(position).getGd_value());
			
			holder.tv_discount_name.setText(content);
			holder.tv_discount_value.setText(value);
			
			if (position == selectedRow) {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(255,219,16));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(255,219,16));
			} else {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(204, 204, 204));
			}
//			
			return convertView;
		}
		
	}
	
	private OnTouchListener onItemTouchListener = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
			}
			return false;
		}
		
	};
	
	public void ScrollToRow(int row){
		selectedRow = row;
		mAdapter.notifyDataSetChanged();
	}

	
	private OnClickMessageListener mOnClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
			TransactionManager.getInstance().SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, mPaymentMgr.TransDntValue * (-1));
			mPaymentMgr.TransDntType = "";
			mPaymentMgr.TransDntValue = 0;
			mPaymentMgr.mSaleListItems.announceCartChanges();
			mDeviceManager.enableAllScanner();
			dismiss();
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {

		}

	};
	

}
